﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using PsychveyApp1.classes;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace PsychveyApp1
{
    public partial class ForgotPassword1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnContinue_Click(object sender, EventArgs e)
        {

            UserAccount us = new UserAccount();
            us.email = tbxEmail.Text;
            if (tbxEmail.Text == "")
            {
                lblMessage.Text = "Please type a valid email address in the text box.";
            }
            else if (DBManager.checkIfUsernameExistByEmail(us.email) == 0)
            {
                lblMessage.Text = "Email address does not exist";
            }
            else if (DBManager.checkIfUsernameExistByEmail(us.email) == 1)
            {
                this.SendPassword(us.username, us.password, us.email);
                lblMessage.Text = "Password sent!";
                DBManager.UpdateUserPassword(Session["randomGeneratedPassword"].ToString(), tbxEmail.Text);
            }
        }

        protected void SendPassword(string username, string password, string email)
        {
            string email1 = Convert.ToString(DBManager.GetEmail(email));
            MailAddress recipient = new MailAddress(email1);
            MailAddress sender = new MailAddress("psychvey@hotmail.com");
            MailMessage message = new MailMessage(sender, recipient);
            message.Subject = "New password for Psychvey Account(" + DBManager.GetUsernameByEmail(email) + ")";
            Session["randomGeneratedPassword"] = generatePassword().ToString();
            message.Body = "Hi " + DBManager.GetUsernameByEmail(email) + ",\n" + "Your new reset password is: " + Session["randomGeneratedPassword"] + "";
            message.IsBodyHtml = true;
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.live.com";
            NetworkCredential cred = new NetworkCredential("psychvey@hotmail.com", "make$$$$$alot", "");
            client.UseDefaultCredentials = false;
            client.Credentials = cred;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            try
            {
                client.Send(message);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        protected string generatePassword()
        {
            string passChar = "abcdefghijklmnopqrstuvwxyz!#$%^&*()ABCDEFGIJKLMNOPQRSTUVWXYZ";
            string pass = "";

            Random rnd = new Random();
            for (int i = 0; i < 8; i++)
            {
                int random = rnd.Next(0, passChar.Length - 1);
                pass += passChar.Substring(random, 1);
            }
            return pass;
        }
    }

}